package com.zulucap.riftcalc;

import java.io.IOException;

import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.content.Intent;
import android.content.res.XmlResourceParser;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.zulucap.R;

public class AddSoulActivity extends BaseActivity {
	Integer mPointsSpent = 0;
	Integer mTotalPointsSpent = 0;
	String mSoulString = "";
	String mSoul = "";
	Integer soulButton = 0;
	String mSouls[] = new String[3];
	int mSoulPointsSpent[] = new int[3];
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_soul);
        
        String calling = getIntent().getExtras().getString("calling");
        mTotalPointsSpent = getIntent().getExtras().getInt("totalPointsSpent");
        mPointsSpent = getIntent().getExtras().getInt("pointsSpent");
        mSoulString = getIntent().getExtras().getString("soulString");
        mSoul = getIntent().getExtras().getString("soul");
        mSoulPointsSpent = getIntent().getExtras().getIntArray("soulPointsSpent");
        
        mSouls = getIntent().getExtras().getStringArray("souls");
        
        // Process souls
        XmlResourceParser souls = getResources().getXml(R.xml.souls);
//        LinearLayout addSoulLayout = (LinearLayout) findViewById(R.id.layout_add_souls);
        
        try {
            processSouls(calling, souls);
        } catch (Exception e) {
            //Log.e(DEBUG_TAG, "Failed to load scores", e);
        }
    }
    
    /**
     * Churn through an XML score information and populate a {@code TableLayout}
     * 
     * @param scoreTable
     *            The {@code TableLayout} to populate
     * @param scores
     *            A standard {@code XmlResourceParser} containing the scores
     * @throws XmlPullParserException
     *             Thrown on XML errors
     * @throws IOException
     *             Thrown on IO errors reading the XML
     */
    private void processSouls(String calling, XmlResourceParser souls) throws XmlPullParserException,
            IOException {
        int eventType = -1;
        String _calling = "";
        Integer index = 0;
        
        
        // Find Score records from XML
        while (eventType != XmlResourceParser.END_DOCUMENT) {
            if (eventType == XmlResourceParser.START_TAG) { 
            	
                // Get the name of the tag (eg souls or soul)
                String strName = souls.getName();
                
                if (strName.equals("calling")){
                	_calling = souls.getAttributeValue(null, "id");
                }
                if (strName.equals("soul") && _calling.equals(calling)) {
                	String soulId = souls.getAttributeValue(null, "id");
                    String soulImage = souls.getAttributeValue(null, "image");
                    
                    Button soul = (Button) findViewById(getResources().getIdentifier("button_soul"  + (index + 1),"id","com.zulucap"));
                   
                    soul.setText(soulId);
                    soul.setCompoundDrawablesWithIntrinsicBounds( 
                    		getResources().getIdentifier(soulImage, "drawable", getPackageName()), 0, 0, 0);
                    
                    Boolean soulSet = false;
                    for(Integer j = 0; j<mSouls.length; j++){
                    	if(soulId.equals(mSouls[j])){
                    		soul.setVisibility(View.INVISIBLE);
                    		soul.setHeight(0);
                    	}
                    		
                    }
                    
                    index++;
                }
            }
            eventType = souls.next();
        }
    }
    
    public void openSoulTree(View view){
    	Intent i = new Intent(getApplicationContext(), SoulTreeActivity.class);
    	Button button = (Button) findViewById(view.getId());
    	String soul = (String) button.getText();
    	
    	
    	// Return Selected Value
    	mSoul = soul;
    	soulButton = getIntent().getExtras().getInt("button");
    	
    	i.putExtra("soul", soul );
    	i.putExtra("newSoul", true);
    	i.putExtra("pointsSpent", mPointsSpent);
    	i.putExtra("totalPointsSpent", mTotalPointsSpent);
    	i.putExtra("soulPointsSpent", mSoulPointsSpent);
    	i.putExtra("souls", mSouls);

    	startActivityForResult(i, 1);
    }
    
    protected void onActivityResult(int requestCode, int resultCode,
            Intent data) {
        if (requestCode == 1) {
            if (resultCode == RESULT_OK) {
                
            	mSoulString = (String) data.getExtras().get("soulString");
                mPointsSpent = data.getExtras().getInt("pointsSpent");
                mTotalPointsSpent = data.getExtras().getInt("totalPointsSpent");
                
             // Return Selected Values
            	Intent resultIntent = new Intent();            	
            	resultIntent.putExtra("soul", mSoul);
            	resultIntent.putExtra("button", soulButton);
            	resultIntent.putExtra("soulString", mSoulString);
            	resultIntent.putExtra("pointsSpent", mPointsSpent);
            	resultIntent.putExtra("totalPointsSpent", mTotalPointsSpent);
            	resultIntent.putExtra("souls", mSouls);
            	
            	
            	setResult(Activity.RESULT_OK, resultIntent);
                this.finish();
            }
        }
    }
}
